<?php
require_once 'vendor/autoload.php';

// 初始化应用
$app = new \think\App();
$app->initialize();

use think\facade\Db;

try {
    echo "创建同步记录表...\n";
    
    $sql = "CREATE TABLE IF NOT EXISTS sync_records (
        id INT AUTO_INCREMENT PRIMARY KEY,
        sync_type VARCHAR(50) NOT NULL COMMENT '同步类型',
        start_date DATE NOT NULL COMMENT '开始日期',
        end_date DATE NOT NULL COMMENT '结束日期',
        total_synced INT DEFAULT 0 COMMENT '同步成功数量',
        total_skipped INT DEFAULT 0 COMMENT '跳过数量',
        duplicate_count INT DEFAULT 0 COMMENT '重复数量',
        error_count INT DEFAULT 0 COMMENT '错误数量',
        error_messages TEXT COMMENT '错误信息',
        sync_status ENUM('running', 'completed', 'failed') DEFAULT 'running' COMMENT '同步状态',
        started_at DATETIME NOT NULL COMMENT '开始时间',
        completed_at DATETIME NULL COMMENT '完成时间',
        duration_seconds INT DEFAULT 0 COMMENT '耗时(秒)',
        created_at DATETIME NULL COMMENT '创建时间',
        updated_at DATETIME NULL COMMENT '更新时间',
        INDEX idx_sync_type (sync_type),
        INDEX idx_start_date (start_date),
        INDEX idx_end_date (end_date),
        INDEX idx_sync_status (sync_status),
        INDEX idx_started_at (started_at)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='同步记录表'";
    
    Db::execute($sql);
    echo "同步记录表创建成功\n";
    
    // 检查表结构
    $columns = Db::query('DESCRIBE sync_records');
    echo "\n同步记录表结构:\n";
    foreach ($columns as $column) {
        echo $column['Field'] . ' - ' . $column['Type'] . ' - ' . $column['Null'] . ' - ' . $column['Key'] . ' - ' . $column['Default'] . ' - ' . $column['Extra'] . "\n";
    }
    
} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
} 